//
//  firstTableviewcell.m
//  Protect -A
//
//  Created by 付剑英 on 16/6/16.
//  Copyright © 2016年 付剑英. All rights reserved.
//

#import "firstTableviewcell.h"
#import "MycollectionView.h"
#import "MycollectionViewCell.h"
#import "firstFlowLayout.h"
#import "ModelOfFirst.h"
#import "baseLabel.h"
#import "FirstDetailViewController.h"
#import <DKNightVersion.h>
@interface firstTableviewcell()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
@property (nonatomic,retain)MycollectionView *collection;
@property (nonatomic,retain)firstFlowLayout *layout;
@property (nonatomic,retain)baseLabel *labelHeader;//头部文字
@end
@implementation firstTableviewcell

//内存管理
-(void)dealloc{
    [_collection release];
    [_arrJieShou release];
    [_layout release];
    [_labelHeader release];
    [super dealloc];
}

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
       [self createcollectionview];
//        self.contentView.dk_backgroundColorPicker = DKColorPickerWithKey(BAR);
    }
    return self;
}
-(void)createcollectionview{
    
    
    self.layout = [[firstFlowLayout alloc]init];
    
    self.collection  = [[MycollectionView alloc]initWithFrame:self.contentView.bounds collectionViewLayout:self.layout];
//    self.collection.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.collection];
    self.collection.delegate = self;
    self.collection.dataSource = self;
    
    
    //注册cell
    
    [self.collection registerClass:[MycollectionViewCell class] forCellWithReuseIdentifier:@"pool"];
   
    
    //注册头部cell
    [self.collection registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"header"];

     [self.collection release];
}


-(void)layoutSubviews{
    
    [super layoutSubviews];
    self.collection.frame =self.contentView.bounds;
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MycollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"pool" forIndexPath:indexPath];
   
    ModelOfFirst *m1 = _arrJieShou[indexPath.row];
    cell.model = m1;
    return  cell;
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 8;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    return CGSizeMake(self.bounds.size.width, 10);
}
#pragma mark - ------------ 头部 ---------------
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    if (kind == UICollectionElementKindSectionHeader) {
        
        UICollectionReusableView *header = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:@"header" forIndexPath:indexPath];
        self.labelHeader = [[baseLabel alloc]initWithFrame:CGRectMake(0, 10, header.bounds.size.width, 50)];
        self.labelHeader.text = @"分类";
        
        _labelHeader.font = [UIFont systemFontOfSize:23];
        _labelHeader.textColor = [UIColor redColor];
        _labelHeader.textAlignment = NSTextAlignmentCenter;
        [header addSubview:self.labelHeader];
        
        return header;
        
    }

    return nil;

}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
  
    //发通知
    
    [[NSNotificationCenter defaultCenter]postNotificationName:@"index" object:@(indexPath.row)];
    

}







#pragma mark - ------------ 重写set 更新collectionview内数组数据 ---------------

-(void)setArrJieShou:(NSArray *)arrJieShou{


    if (_arrJieShou !=arrJieShou) {
        [_arrJieShou release];
        _arrJieShou = [arrJieShou retain];
    }
    [_collection reloadData];}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}



@end
